// ============================== 导入依赖 ============================== //

// ============================== 导出工具 ============================== //

export default {
  // —————————————————————————————— 设置答卷 —————————————————————————————— //

  setAnswer(obj, questionList, answerList = []) {
    questionList.forEach(({ id, no, type, optionList, questionList: subList }) => {
      if (subList.length) { // 有子问题的
        subList.forEach(({ id: subId, no: subNo, questionList: subSubList }) => {
          if (type === 'textareaTable') { // 表格问答
            subSubList.forEach(({ id: subSubId, no: subSubNo }) => {
              const matched = answerList.filter(({ questionId }) => questionId === subSubId)
              obj[`no_${no}_${subSubNo}`] = (matched[0] || {}).value || ''
            })
          } else { // 表格单选、多选
            const matched = answerList.filter(({ questionId }) => questionId === subId)
            const labelList = matched.map(({ optionId }) => (optionList.find(({ id }) => id === optionId) || {}).label)
            if (type === 'radioTable') obj[`no_${no}_${subNo}`] = labelList[0] || undefined
            if (type === 'checkboxTable') obj[`no_${no}_${subNo}`] = labelList.join(' ')
          }
        })
      } else { // 无子问题的
        const matched = answerList.filter(({ questionId }) => questionId === id)
        const valueList = matched.map(({ value }) => value)
        const labelList = matched.map(({ optionId }) => (optionList.find(({ id }) => id === optionId) || {}).label)

        if (type === 'radio') obj[`no_${no}`] = labelList[0] || undefined
        if (type === 'checkbox') obj[`no_${no}`] = labelList.filter((obj) => obj).join(' ')
        if (type === 'textarea') obj[`no_${no}`] = valueList[0] || ''
        if (type === 'rate') obj[`no_${no}`] = valueList[0] || undefined
      }
    })
  },
}
